<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付金额" prop="paymoney">
        <el-input v-model="queryParams.paymoney" placeholder="请输入支付金额" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="orderStart">
        <el-date-picker clearable v-model="queryParams.orderStart" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择订单开始时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="服务名称" prop="serviceName">
        <el-input v-model="queryParams.serviceName" placeholder="请输入服务名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="优惠券ID" prop="couponId">
        <el-input v-model="queryParams.couponId" placeholder="请输入优惠券ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable v-model="queryParams.payTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="店员ID" prop="employeeId">
        <el-input v-model="queryParams.employeeId" placeholder="请输入店员ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['shop:order:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['shop:order:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="支付金额" align="center" prop="paymoney" />
      <el-table-column label="付款类型" align="center" prop="paymentType"width="100"  />
      <el-table-column label="订单开始时间" align="center" prop="orderStart" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单结束时间" align="center" prop="orderOver" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderOver, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单暂停时间" align="center" prop="orderPause" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderPause, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="优惠券ID" align="center" prop="couponId" />
      <el-table-column label="优惠券金额" align="center" prop="couponPrice"width="180" />
      <el-table-column label="支付状态" align="center" prop="payStatus" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺名" align="center" prop="shopName" />
      <el-table-column label="店员ID" align="center" prop="employeeId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:order:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['shop:order:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户Id" />
        </el-form-item>
        <el-form-item label="支付金额" prop="paymoney">
          <el-input v-model="form.paymoney" placeholder="请输入支付金额精确到2位小数" />
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <el-input v-model="form.payStatus" placeholder="请输入支付金额精确到2位小数" />
        </el-form-item>paymentType
        <el-form-item label="付款类型" prop="paymentType">
          <el-input v-model="form.paymentType" placeholder="请输入支付金额精确到2位小数" />
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-input v-model="form.orderStatus" placeholder="请输入支付金额精确到2位小数" />
        </el-form-item>
        <el-form-item label="开始时间" prop="orderStart">
          <el-date-picker clearable v-model="form.orderStart" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择订单开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="orderOver">
          <el-date-picker clearable v-model="form.orderOver" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择订单结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="暂停时间" prop="orderPause">
          <el-date-picker clearable v-model="form.orderPause" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择订单暂停时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="优惠券ID" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠券ID" />
        </el-form-item>
        <el-form-item label="优惠券金额" prop="couponPrice">
          <el-input v-model="form.couponPrice" placeholder="请输入优惠券金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="form.payTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺名" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名" />
        </el-form-item>
        <el-form-item label="店员ID" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入店员ID" />
        </el-form-item>
<!--        <el-form-item label="订单状态" prop="orderStatus">-->
<!--          <el-input v-model="form.orderStatus" placeholder="请输入订单状态：未开始/进行中/已结束" />-->
<!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/shop/order";
import { listShop } from "@/api/shop/shop";
import useUserStore from '@/store/modules/user';
const { proxy } = getCurrentInstance();
const userStore = useUserStore();
const orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId:null,
    userName: null,
    paymoney: null,
    paymentType: null,
    orderStart: null,
    orderOver: null,
    orderPause: null,
    serviceName: null,
    orderStatus: null,
    couponId: null,
    couponPrice: null,
    payStatus:null,
    payTime: null,
    shopName: null,
    employeeId: null,
  },
  queryShopParams: {
    pageNum: 1,
    pageSize: 10,
    shopName: null,
    shopDesc: null,
    shopNumber: null,
    shopStatus: null,
    shopTimeStart: null,
    shopPic: null,
    shopAddress: null,
    phoneNumber: null,
    employeeName: null,
    bossName: null
  },
  rules: {

    userName:[
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    userId:[
      { required: true, message: "用户名id", trigger: "blur" }
    ],
    paymoney: [
      { required: true, message: "支付金额不能为空", trigger: "blur" }
    ],

    orderStart: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    orderOver: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
    orderPause: [
      { required: true, message: "暂停时间不能为空", trigger: "blur" }
    ],
    serviceName:[
      { required: true, message: "服务名称不能为空", trigger: "blur" }
    ],

    couponId:[
      { required: true, message: "优惠劵ID不能为空", trigger: "blur" }
    ],
    couponPrice: [
      { required: true, message: "优惠劵金额不能为空", trigger: "blur" }
    ],
    orderStatus:[
      { required: true, message: "订单状态不能为空", trigger: "blur" }
    ],
    payStatus:[
      { required: true, message: "支付状态不能为空", trigger: "blur" }
    ],
    payTime:[
      { required: true, message: "支付时间不能为空", trigger: "blur" }
    ],
    shopName:[
      { required: true, message: "店铺名不能为空", trigger: "blur" }
    ],
    employeeId: [
      { required: true, message: "店员ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, queryShopParams, form, rules } = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  if (userStore.roles[0] == "admin") {
    listOrder(queryParams.value).then(response => {
      orderList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  } else if (userStore.roles[0] == "shopManager") {
    // 如果角色为店铺管理员
    queryShopParams.value.employeeName = userStore.name;
    // 查询出管理员管理的店铺
    listShop(queryShopParams.value).then(response => {
      queryParams.value.shopName = response.rows[0].shopName;
      total.value = response.total;
      loading.value = false;
      // 查询出该店铺的订单
      listOrder(queryParams.value).then(response => {
        orderList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
    }).catch(error => {
      console.log(error);
    });
  } else if (userStore.roles[0] == "boss") {
    queryShopParams.value.bossName = userStore.name;
    // 查询旗下店铺
    listShop(queryShopParams.value).then(response => {
      // 根据旗下店铺查询出所有订单
      for (const shop in response.rows) {
        queryParams.value.shopName = response.rows[shop].shopName;
        listOrder(queryParams.value).then(response => {
          // 遍历查询结果封装入订单查询列表
          response.rows.forEach(element => {
            orderList.value.push(element);
          });
          total.value = response.total;
          loading.value = false;
        });
      }
    }).catch(error => {
      console.log(error);
    });
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    userName: null,
    paymoney: null,
    paymentType: null,
    createTime: null,
    orderStart: null,
    orderOver: null,
    orderPause: null,
    serviceName: null,
    orderStatus: null,
    couponId: null,
    couponPrice: null,
    payStatus: null,
    payTime: null,
    shopName: null,
    employeeId: null,
    updateTime: null
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _orderId = row.orderId || ids.value
  getOrder(_orderId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate(valid => {
    if (valid) {
      if (form.value.orderId != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value;
  proxy.$modal.confirm('是否确认删除订单编号为"' + _orderIds + '"的数据项？').then(function () {
    return delOrder(_orderIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shop/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

getList();
</script>
